package org.example.remote;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.example.internalCommon.constant.AmapConfigConstants;
import org.example.internalCommon.request.ForecastPriceDTO;
import org.example.internalCommon.response.DirectionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class MapDicDistrictClient {

    @Value("${amap.key}")
    private String amapKey;

    @Autowired
    private RestTemplate restTemplate;

    public String dicDistrict(String keywords){

        // &key=<用户的key>
        // 拼装请求的url
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.DISTRICT_URL);
        url.append("?");
        url.append("keywords="+keywords);
        url.append("&");
        url.append("subdistrict=3");
        url.append("&");
        url.append("key="+amapKey);

        ResponseEntity<String> forEntity = restTemplate.getForEntity(url.toString(), String.class);

        return forEntity.getBody();
    }

    public DirectionResponse dicDistrict(ForecastPriceDTO forecastPriceDTO){
        // 组装参数 调用url
        StringBuilder urlBuild = new StringBuilder();
        urlBuild.append(AmapConfigConstants.DIRECTION_URL);
        urlBuild.append("?");
        urlBuild.append("origin=" + forecastPriceDTO.getDepLongitude() + "," + forecastPriceDTO.getDepLatitude());
        urlBuild.append("&");
        urlBuild.append("destination=" + forecastPriceDTO.getDestLongitude() + "," + forecastPriceDTO.getDestLatitude());
        urlBuild.append("&");
        urlBuild.append("extensions=base");
        urlBuild.append("&");
        urlBuild.append("output=json");
        urlBuild.append("&");
        urlBuild.append("key=" + amapKey);
        System.out.println(urlBuild);
        // 调用高德接口
        ResponseEntity<String> forEntity = restTemplate.getForEntity(urlBuild.toString(), String.class);
        // 解析接口
        String body = forEntity.getBody();
        System.out.println("-------------------------");
        System.out.println(body);
        return parseDirectionEntity(body);
    }


    private DirectionResponse parseDirectionEntity(String directionString){
        DirectionResponse directionResponse = null;
        try {
            directionResponse = new DirectionResponse();
            JSONObject result = JSONObject.fromObject(directionString);
            int status = result.getInt(AmapConfigConstants.STATUS);
            System.out.println("---------status:"+status);
            if (status == 1) {
                JSONObject route = result.getJSONObject(AmapConfigConstants.ROUTE);
                JSONArray paths = route.getJSONArray(AmapConfigConstants.PATHS);
                JSONObject path = paths.getJSONObject(0);
                if (path.has(AmapConfigConstants.DURATION)) {
                    directionResponse.setDuration(path.getInt(AmapConfigConstants.DURATION));
                }
                if (path.has(AmapConfigConstants.DISTANCE)) {
                    directionResponse.setDistance(path.getInt(AmapConfigConstants.DISTANCE));
                }
            }
        }catch (Exception e){

        }
        return directionResponse;
    }
}
